﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using AutoCAD;

namespace cadAutoPlotSample
{
    class Program
    {
        private static AcadApplication cadApp;
        private static AcadModelSpace modelSpace;
        private static AcadDocument pDoc;
        private static string outputFile;
        private static string previewFile;
        private static string drawType;
        private static string drawParams;
        private static Bitmap preview;
        private static Graphics graph;
        private static Pen cadPen;
        private static void drawCad(AcadModelSpace modelSpace, Graphics cadGraph,string drawType,string drawParams)
        {
            drawParams = drawParams.Replace("[", "").Replace("]", "");
            switch (drawType)
            {
                case "rect":
                    string[] whparams = drawParams.Split(',');
                    float width = Convert.ToSingle(whparams[0]);
                    float height = Convert.ToSingle(whparams[1]);
                    double[] rectPts = new double[] { -width / 2, height / 2, 0, width / 2, height / 2, 0, width/2, -height/2, 0, -width/2,-height/2,0, -width / 2, height / 2, 0 };
                    modelSpace.AddPolyline(rectPts);
                    cadGraph.DrawRectangle(Pens.Black, -width / 2, height / 2, width, height);
                    break;
                case "circle":
                    float radius = Convert.ToSingle(drawParams);
                    double[] center = { 0, 0, 0 };
                    modelSpace.AddCircle(center, radius);
                    cadGraph.DrawEllipse(Pens.Black, -radius / 2, radius / 2, radius, radius);
                    break;
                default:
                    break;
            }
        }
        private static int Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("params error");
                return -1;
            }
            drawType = args[0];
            drawParams = args[1];
            outputFile = args[2];
            previewFile = args[2];
            try
            {
                cadApp = new AcadApplication();
                cadApp.Visible = false;
                pDoc = cadApp.ActiveDocument;
                modelSpace = pDoc.ModelSpace;

                preview = new Bitmap(2000, 2000);
                graph = Graphics.FromImage(preview);
                cadPen = new Pen(Color.Black);
                graph.Clear(Color.White);
                graph.TranslateTransform(1000, 1000);
                drawCad(modelSpace, graph, drawType, drawParams);

                pDoc.SaveAs(outputFile);
                pDoc.Close();
                cadApp.Quit();

                previewFile = previewFile.Substring(0, previewFile.LastIndexOf(".")) + ".jpg";
                preview.Save(previewFile, System.Drawing.Imaging.ImageFormat.Jpeg);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                pDoc.SaveAs(outputFile);
                pDoc.Close();
                cadApp.Quit();
                Console.ReadKey();
                return -1;
            }
            return 0;
        }

    }
}
